package com.kaishengit.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.pojo.Project;
import com.kaishengit.pojo.User;
import com.kaishengit.pojo.Userproject;
import com.kaishengit.service.ProjectService;
import com.kaishengit.service.UserService;
import com.kaishengit.service.UserprojectService;

@Results({
	@Result(name="list",location="project.action",type="redirectAction")
})
public class ProjectAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private List<User> userList;
	private Project project;
	private ProjectService projectService;
	private String selectname;
	private String member;
	private List<Project> projectList;
	private User user;
	private List<Userproject> UserprojectList;
	private UserprojectService userprojectService;
	
	public String list() throws Exception {
		user = (User) getFromSession("user");
		if("admin".equals(user.getUsername())){
			projectList = projectService.findAll();
		}else{
			UserprojectList=userprojectService.findByUser(user);
		}
		return "success";
	}
	
	@Action("saveproject")
	public String add() throws Exception {
		projectService.save(project,selectname,member);
		return LIST;
	}
	
	@Action("createproject")
	public String create() throws Exception {
		userList = userService.findAll();
		return "success";
	}
	
	@Action("editproject")
	public String edit() throws Exception {
		project = (Project) getFromSession("project");
		return "success";
	}
	
	@Action("updateproject")
	public String update() throws Exception {
		projectService.update(project);
		return LIST;
	}
	
	
	
	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
	
	public ProjectService getProjectService() {
		return projectService;
	}
	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public String getSelectname() {
		return selectname;
	}

	public void setSelectname(String selectname) {
		this.selectname = selectname;
	}

	public String getMember() {
		return member;
	}

	public void setMember(String member) {
		this.member = member;
	}

	public List<Project> getProjectList() {
		return projectList;
	}
	
	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public void setApplication(Map<String, Object> arg0) {
	}

	public List<Userproject> getUserprojectList() {
		return UserprojectList;
	}

	public void setUserprojectList(List<Userproject> userprojectList) {
		UserprojectList = userprojectList;
	}
	
	@Autowired
	public void setUserprojectService(UserprojectService userprojectService) {
		this.userprojectService = userprojectService;
	}
	
}
